//
// Created on 2024/3/18.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef OHOS_VIDEOCALLBACKHANDLER_H
#define OHOS_VIDEOCALLBACKHANDLER_H
#include "napi/video/VideoPlayerInfo.h"
#include "napi/helper/NapiHelper.h"
#include "napi/video/GlobalInfoMap.h"
class VideoPlayerHandler{
    public:
        /***--------------------callback回调-----------------------**/
        virtual void onPreparedEvent(int32_t id,VideoPlayerInfo* info) = 0;
        virtual void onEndedEvent(int32_t id,VideoPlayerInfo* info) = 0;
        virtual void onFailedEvent(int32_t id,VideoPlayerInfo* info) = 0;
    
        void createVideoPlayer(int32_t id, int32_t x,int32_t y,int32_t w,int32_t h){
            JSFunction::getFunction("VideoPlayer.createVideoPlayer").invoke<void>(id, x, y, w, h);
            VideoPlayerInfo *videoPlayer = new VideoPlayerInfo();
            videoPlayer->viewTag = id;
            GlobalInfoMap::getInstance()->addVideoPlayerInfo(id, videoPlayer);
        }
        
        void setVideoPlayerRect(int32_t id,int32_t x,int32_t y,int32_t w,int32_t h){
            JSFunction::getFunction("VideoPlayer.setVideoPlayerRect").invoke<void>(id, x, y, w, h);
        }

        void setVideoPlayerUrl(int32_t id, const char * source) {
            JSFunction::getFunction("VideoPlayer.setURL").invoke<void>(id, source, 1);
            VideoPlayerInfo *videoPlayerInfo = GlobalInfoMap::getInstance()->getVideoPlayerInfo(id);
            videoPlayerInfo->url = std::string(source);
        }

        void playVideoPlayer(int32_t id) { JSFunction::getFunction("VideoPlayer.play").invoke<void>(id); }
        void pauseVideoPlayer(int32_t id) { JSFunction::getFunction("VideoPlayer.pause").invoke<void>(id); }
        void seekToVideoPlayer(int32_t id,int32_t seekTo) { JSFunction::getFunction("VideoPlayer.seekTo").invoke<void>(id,seekTo); }
        void removeVideoPlayer(int32_t id) {
            JSFunction::getFunction("VideoPlayer.removeVideoPlayer").invoke<void>(id);
            GlobalInfoMap::getInstance()->deleteVideoByTag(id);
        }

        static  VideoPlayerHandler* getInstance(){
            return instance;
        }
    protected:
        static VideoPlayerHandler* instance;
};
#endif //OHOS_VIDEOCALLBACKHANDLER_H
